﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Photography.App_Data;

namespace Photography.Admin
{
    public partial class ManageGalleries : System.Web.UI.Page
    {
        private List<int> _orderedImagesIds = new List<int>();
        //private Dictionary<int, Image> _images = new Dictionary<int, Image>();

        private void AddImage(Photography.App_Data.GetGalleryPhotosResult row)
        {
            _orderedImagesIds.Add(row.ID);
            //_images.Add(photoId, new Image( FromFile(photoUrl));
        }
        private void AddImage(Photography.App_Data.GetSlideShowPhotosResult row)
        {
            _orderedImagesIds.Add(row.ID);
            //_images.Add(photoId, new Image( FromFile(photoUrl));
        }

        protected int? GalleryId
        {
            get
            {
                return string.IsNullOrEmpty(Request["GalleryID"]) ? (int?)null : int.Parse(Request["GalleryID"]);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!(Master as Photography.Admin.AdminMaster).AdminID.HasValue)
                Response.Redirect("Default.aspx");
        }

        protected void GetGalleries(object sender, LinqDataSourceSelectEventArgs e)
        {
            Photography.App_Data.PhotographyDataContext context = new Photography.App_Data.PhotographyDataContext();
            e.Result = context.GetPublicGalleries();
        }

        protected void GetPhotos(object sender, LinqDataSourceSelectEventArgs e)
        {
            if (GalleryId.HasValue)
            {
                Photography.App_Data.PhotographyDataContext context = new Photography.App_Data.PhotographyDataContext();
                System.Data.Linq.ISingleResult<Photography.App_Data.GetGalleryPhotosResult> data = context.GetGalleryPhotos(GalleryId);
                /*foreach (Photography.App_Data.GetGalleryPhotosResult photo in data)
                    AddImage(photo.ID, photo.Location);*/
                e.Result = data;
           }
            else
            {
                Photography.App_Data.PhotographyDataContext context = new Photography.App_Data.PhotographyDataContext();
                System.Data.Linq.ISingleResult<Photography.App_Data.GetSlideShowPhotosResult> data = context.GetSlideShowPhotos();
                /*foreach (Photography.App_Data.GetSlideShowPhotosResult photo in data)
                    AddImage(photo.ID, photo.Location);*/
                e.Result = data;
            }
        }

        protected void btnAddPhoto_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Request["GalleryID"]))
                Response.Redirect("Photo.aspx");
            else
                Response.Redirect("Photo.aspx?GalleryID=" + Request["GalleryID"]);
        }

        protected void btnAddGallery_Click(object sender, EventArgs e)
        {
            Response.Redirect("Gallery.aspx");
        }

        protected void btnEditGallery_Click(object sender, EventArgs e)
        {
            Response.Redirect("Gallery.aspx?GalleryID=" + Request["GalleryID"]);
        }

        protected void grdPhotos_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            Photography.App_Data.PhotographyDataContext context = new Photography.App_Data.PhotographyDataContext();

            int photoId = Convert.ToInt32(e.CommandArgument);
 
            if (e.CommandName == "DeletePhoto")
            {
                if (string.IsNullOrEmpty(Request["GalleryID"]))
                {
                    context.UpdatePhoto(photoId, null, null, null, null, null, false, null);
                    lblNotification.Text = "Photo #" + photoId.ToString() + " was successfuly removed from slideshow";
                }
                else
                {
                    try
                    {
                        string location = MapPath((from a in context.GetGalleryPhotos(int.Parse(Request["GalleryID"])) where a.ID == photoId select a.Location).First());
                        System.IO.File.Delete(location);
                    }
                    catch
                    {
                    }

                    context.DeletePhoto(photoId);
                    lblNotification.Text = "Photo #" + photoId.ToString() + " was successfuly deleted from gallery";
                }
            }
            else if (e.CommandName == "EditPhoto")
            {
                if (string.IsNullOrEmpty(Request["GalleryID"]))
                    Response.Redirect("Photo.aspx?PhotoID=" + photoId.ToString());
                else
                    Response.Redirect("Photo.aspx?GalleryID=" + Request["GalleryID"] + "&PhotoID=" + photoId.ToString());
            }
            else if (e.CommandName == "MoveUp" || e.CommandName == "MoveDown")
            {
                List<int> photos = new List<int>();
                if (string.IsNullOrEmpty(Request["GalleryID"]))
                    foreach (GetSlideShowPhotosResult photo in context.GetSlideShowPhotos())
                        photos.Add(photo.ID);
                else
                    foreach (GetGalleryPhotosResult photo in context.GetGalleryPhotos(int.Parse(Request["GalleryID"])))
                        photos.Add(photo.ID);

                int indexOfPhotoToMove = photos.IndexOf(photoId);
                photos.RemoveAt(indexOfPhotoToMove);

                if (e.CommandName == "MoveDown")
                {
                    if (indexOfPhotoToMove == photos.Count)
                        photos.Insert(0, photoId);
                    else if (indexOfPhotoToMove + 1 == photos.Count)
                        photos.Add(photoId);
                    else
                        photos.Insert(indexOfPhotoToMove + 1, photoId);
                }
                else
                {
                    if (indexOfPhotoToMove == 0)
                        photos.Add(photoId);
                    else
                        photos.Insert(indexOfPhotoToMove - 1, photoId);
                }

                for (int i = 0; i < photos.Count; i++)
                    context.UpdatePhoto(photos[i], null, null, string.IsNullOrEmpty(Request["GalleryID"]) ? null : (int?)(i + 1), null, null, null, string.IsNullOrEmpty(Request["GalleryID"]) ? (int?)(i + 1) : null);
            }

            grdPhotos.DataBind();
        }

        protected void grdPhotos_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int photoId = 0;
                if (e.Row.DataItem is Photography.App_Data.GetGalleryPhotosResult)
                    photoId = (e.Row.DataItem as Photography.App_Data.GetGalleryPhotosResult).ID;
                else if  (e.Row.DataItem is Photography.App_Data.GetSlideShowPhotosResult)
                    photoId = (e.Row.DataItem as Photography.App_Data.GetSlideShowPhotosResult).ID;

                ((LinkButton)e.Row.Cells[4].Controls[0]).CommandArgument = photoId.ToString();
                ((LinkButton)e.Row.Cells[5].Controls[0]).CommandArgument = photoId.ToString();
                ((LinkButton)e.Row.Cells[6].Controls[0]).CommandArgument = photoId.ToString();
                ((LinkButton)e.Row.Cells[7].Controls[0]).CommandArgument = photoId.ToString();
            }
        }

        protected void grdPhotos_RowDataBound(object sender, GridViewRowEventArgs e)
        {
        }
    }
}